<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="150px"
      v-loading="formLoading"
    >
      <el-form-item label="角色名字" prop="name">
        <el-input v-model="formData.name" placeholder="请输入角色名字" />
      </el-form-item>
      <el-form-item label="介绍说明" prop="intro">
        <el-input v-model="formData.intro" placeholder="请输入介绍 说明" />
      </el-form-item>
      <el-form-item label="提示词" prop="prompt">
        <el-input v-model="formData.prompt" placeholder="请输入提示词" />
      </el-form-item>
      <el-form-item label="声纹" prop="audioToneId">
        <el-select v-model="formData.audioToneId" placeholder="请选择声纹">
          <el-option v-for="item in audioToneList" :key="item.id" :value="item.id" :label="item.name" />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="音色" prop="voice">-->
<!--        <el-input v-model="formData.voice" placeholder="请输入音色" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="音色文件path" prop="voicePath">-->
<!--        <el-input v-model="formData.voicePath" placeholder="请输入音色文件path" />-->
<!--      </el-form-item>-->
      <el-form-item label="短期记忆消息数量" prop="shortMemoryMessageSize">
        <el-input-number v-model="formData.shortMemoryMessageSize" placeholder="请输入短期记忆消息数量" />
      </el-form-item>
      <el-form-item label="图片" prop="roleImageId">
        <UploadImg @uploadImgSuccess="uploadSuccess" v-model="formData.roleImageUrl" height="80px" />
      </el-form-item>
      <el-form-item label="头像" prop="roleIconId">
        <UploadImg @uploadImgSuccess="roleIconUploadSuccess" v-model="formData.roleIconUrl" height="80px" />
      </el-form-item>
      <el-form-item label="关联标签" prop="tagIdArray">
        <el-select multiple v-model="formData.tagIdArray" placeholder="请输入角色关联的标签">
          <el-option v-for="item in tagList" :key="item.id" :value="item.id" :label="item.tagName" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { AudioToneApi } from '@/api/resource/audiotone';
import { CosplayApi, CosplayVO } from '@/api/lm/cosplay'
import {TagApi} from "@/api/lm/tag";

/** 角色 表单 */
defineOptions({ name: 'CosplayForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const tagList = ref([]) // 标签列表
const audioToneList = ref([]) // 声纹列表
const formData = ref({
  id: undefined,
  name: undefined,
  intro: undefined,
  prompt: undefined,
  audioToneId: undefined,
  voice: undefined,
  voicePath: undefined,
  shortMemoryMessageSize: undefined,
  roleImageId: undefined,
  roleIconId: undefined,
  roleImageUrl: undefined,
  roleIconUrl: undefined,
  tagIdArray: undefined
})
const formRules = reactive({
  name: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number, tagArray:any[]) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  tagList.value = tagArray;
  resetForm()
  const audioToneData = await AudioToneApi.getAudioTonePage({pageNo: 1, pageSize: 100});
  audioToneList.value = audioToneData.list;
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      let res =  await CosplayApi.getCosplay(id);
      res.tagIdArray = res.tagArrayVo ? res.tagArrayVo.map(e=> {
        return e.id;
      }) : [];
      if(res.roleImage) {
        res.roleImageUrl = res.roleImage.fileUrl;
      }
      if(res.roleIcon) {
        res.roleIconUrl = res.roleIcon.fileUrl;
      }
      formData.value = res
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const uploadSuccess = (e)=> {
  console.log(e);
  formData.value.roleImageId = e.fileId;
  formData.value.roleImageUrl = e.fileUrl;
}

const roleIconUploadSuccess = (e)=> {
  console.log(e);
  formData.value.roleIconId = e.fileId;
  formData.value.roleIconUrl = e.fileUrl;
}

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data: any = formData.value
    if(!data.roleImageUrl) {
      data.roleImageId = "";
      data.roleImage = null;
    }
    if(!data.roleIconUrl) {
      data.roleIconId = "";
      data.roleIcon = null;
    }
    if (formType.value === 'create') {
      await CosplayApi.createCosplay(data)
      message.success(t('common.createSuccess'))
    } else {
      await CosplayApi.updateCosplay(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    name: undefined,
    prompt: undefined,
    voice: undefined,
    voicePath: undefined,
    audioToneId: undefined,
    shortMemoryMessageSize: undefined,
    intro: undefined,
    roleImageId: undefined,
    roleIconId: undefined,
    tagIdArray: undefined
  }
  formRef.value?.resetFields()
}
</script>
